<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <!-- 头部搜素start -->
      <el-form
        :model="query"
        @submit.native.prevent
        ref="queryForm"
        v-show="showSearch"
        :inline="true"
        label-width="65px"
      >
        <el-form-item label="操作类型" prop="method">
          <el-select v-model="query.method">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="登录" value="LOGIN"></el-option>
            <el-option label="登出" value="LOGIN_OUT"></el-option>
            <el-option label="重置密码" value="RESET_PWD"></el-option>
            <el-option label="找回密码" value="FIND_PWD"></el-option>
            <el-option label="修改密码" value="UPDATE_PWD"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名" prop="userName">
          <common-input
            v-model="query.userName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="地址" prop="ipaddr">
          <common-input v-model="query.ipaddr" clearable @enter="handleQuery" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="query.status">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="成功" value="0"></el-option>
            <el-option label="失败" value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="操作时间" prop="daterange">
          <el-date-picker
            v-model="query.daterange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            align="right"
          ></el-date-picker>
        </el-form-item>

        <common-dept
          ref="commonDept"
          :currentParent="currentParent"
          @getDeptId="getDeptId"
          @getDeptList="getDeptList"
          @getLoading="getLoading"
        ></common-dept>
      </el-form>
    </common-search>
    <!-- 头部搜素end -->

    <!--列表 start-->
    <common-table slot="content" v-loading="loading" :data="list">
      <el-table-column label="用户名" align="left" prop="userName">
        <template slot-scope="{ row }">{{ row.userName }}</template>
      </el-table-column>

      <el-table-column label="地址" align="left" prop="ipaddr">
        <template slot-scope="{ row }">{{ row.ipaddr }}</template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <el-tag type="success" v-if="row.status == 0">成功</el-tag>
          <el-tag type="danger" v-else>失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作类型" align="left" prop="method">
        <template slot-scope="{ row }">
          <span v-if="row.method == 'LOGIN'">登录</span>
          <span v-if="row.method == 'LOGIN_OUT'">登出</span>
          <span v-if="row.method == 'RESET_PWD'">重置密码</span>
          <span v-if="row.method == 'FIND_PWD'">找回密码</span>
          <span v-if="row.method == 'UPDATE_PWD'">修改密码</span>
        </template>
      </el-table-column>
      <el-table-column
        label="组织"
        align="left"
        prop="deptName"
        width="180"
        show-overflow-tooltip
      />

      <el-table-column label="描述" align="left" prop="msg">
        <template slot-scope="{ row }">{{ row.msg }}</template>
      </el-table-column>

      <el-table-column label="操作时间" align="left" prop="accessTime" width="180">
        <template slot-scope="{ row }">{{ row.accessTime }}</template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />
  </common-list>
</template>

<script>
import { loginlog_list } from "@/api/management/logs/logs.js";
import BaseList from "@/mixins/BaseList";
import { getSingleArray, getFullDept } from "@/utils/util";

export default {
  mixins: [BaseList],
  components: {},
  props: {},
  data() {
    return {
      filterMenu: "", //菜单筛选
      showSearch: true, // 查询部分显示
      total: 1, // 总条数
      loading: false, // table 加载中
      list: [], // 数据列
      query: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        ipaddr: null,
        status: null,
        daterange: null,
        method: null,
        deptIds: null,
      }, // 查询条件
      currentParent: 1,
      singleDeptList: [],
    };
  },
  watch: {},
  computed: {},
  methods: {
    getLoading(){
      this.loading = true;
    },
    getDeptList(list) {
      this.singleDeptList = getSingleArray(list);
      this.getList();
    },
    getDeptId(val) {
      this.query.deptIds = val;
    },
    getDeptName(val) {
      let dept = this.singleDeptList.find((item) => item.id === val.toString());
      if (dept) return getFullDept(dept.fulllabel);
    },

    /**
     * 获取列表
     */
    getList() {
      this.loading = true;
      let query = { ...this.query };
      loginlog_list(query)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.list.forEach(item=>{
              item.deptName = this.getDeptName(item.deptId || "")
            })
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * 查询
     */
    handleQuery() {
      if (!!this.query.daterange) {
        this.query.beginTime = this.query.daterange[0];
        this.query.endTime = this.query.daterange[1];
      } else {
        this.query.beginTime = "";
        this.query.endTime = "";
      }
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询
     */
    resetQuery() {
      this.query = this.$options.data().query;
      this.$refs.commonDept.clearDept();
    },
  },
  created() {
    // this.getList();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.container {
  .list-box {
    clear: both;
    height: auto;
    overflow: hidden;
    padding: 20px;
  }
}
</style>
